package qq2564874169.tuaotuao.fx.lock;


import qq2564874169.tuaotuao.fx.FxAutoCloseable;
import qq2564874169.tuaotuao.fx.utils.ExUtil;

public interface Lockspace {
    /**
     * 队列锁 - 单个排队进入
     */
    FxAutoCloseable queueLock(String key, Integer abortTimeout) throws AbortTimeoutException;

    /**
     * 异常锁 - 进入失败直接异常
     */
    FxAutoCloseable exLock(String key) throws LockspaceException;

    /**
     * 并发锁 - 多个排队进入
     */
    FxAutoCloseable concurrentLock(String key, int max, Integer abortTimeout) throws AbortTimeoutException;

    default FxAutoCloseable concurrentLock(String key, int max) {
        try {
            return concurrentLock(key, max, null);
        } catch (AbortTimeoutException e) {
            throw ExUtil.toRuntime(e);
        }
    }

    default FxAutoCloseable queueLock(String key) {
        try {
            return queueLock(key, null);
        } catch (AbortTimeoutException e) {
            throw ExUtil.toRuntime(e);
        }
    }
}
